/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.web;

import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.core.auth.domain.WebMenu;
import com.wallellen.core.time.TimeTool;
import com.wallellen.util.spring.BeanFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by walle on 12/9/15.
 */
public class WebUtils {
    protected Object BEAN(String beanIndex) {
        return BeanFactory.getBean(beanIndex);
    }

    public List<WebMenu> getCurrentUserMenus(HttpSession session) {
        List menuList = (List) session.getAttribute("USERMENU");

        return menuList;
    }

    public LoginUser getCurrentUser(HttpSession session) {
        LoginUser user = (LoginUser) session.getAttribute("USEROBJ");

        return user;
    }

    public LoginUser getCurrentUserByDebug(HttpSession session) {
        LoginUser user = new LoginUser() {
            public String getName() {
                return "测试";
            }

            public String getLoginName() {
                return "debug";
            }

            public String getId() {
                return "debug";
            }
        };
        return user;
    }

    public LoginUser setCurrentUser(LoginUser user, HttpSession session) {
        session.setAttribute("USEROBJ", user);
        String photoid = null;
        if ((photoid != null) && (photoid.trim().length() > 0)) ;
        return user;
    }

    public void clearCurrentUser(HttpSession session) {
        session.setAttribute("USEROBJ", null);
    }

    public void setCurrentUserAction(Set<String> userAction, HttpSession session) {
        session.setAttribute("USERACTION", userAction);
    }

    public void setLoginTime(HttpSession session) {
        session.setAttribute("LOGINTIME", TimeTool.getTimeDate14());
    }

    public String getLoginTime(HttpSession session) {
        return (String) session.getAttribute("LOGINTIME");
    }

    public void setCurrentUserMenu(List<WebMenu> userMenu, HttpSession session) {
        session.setAttribute("USERMENU", userMenu);
    }

    public HttpSession getSession(HttpSession httpSession) {
        return httpSession;
    }

    public String getCurrentIp(HttpServletRequest httpRequest) {
        return httpRequest.getRemoteAddr();
    }

    public void setCookie(String cookieName, String value, HttpServletResponse httpResponse) {
        Cookie cookie = new Cookie(cookieName, value);
        int expireday = 2592000;
        cookie.setMaxAge(expireday);
        httpResponse.addCookie(cookie);
    }

    public void delCookie(String cookieName, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        if ((cookieName == null) || (cookieName.equals(""))) {
            return;
        }
        Cookie[] cookies = httpRequest.getCookies();
        int length = 0;

        if ((cookies != null) && (cookies.length > 0)) {
            length = cookies.length;
            for (int i = 0; i < length; i++) {
                String cname = cookies[i].getName();
                if ((cname != null) && (cname.equals(cookieName))) {
                    String cValue = cookies[i].getValue();
                    setCookie(cname, cValue, httpResponse);
                }
            }
        }
    }

    public String getCookieValue(String cookieName, HttpServletRequest httpRequest) {
        if ((cookieName == null) || (cookieName.equals(""))) {
            return null;
        }
        Cookie[] cookies = httpRequest.getCookies();
        int length = 0;

        if ((cookies != null) && (cookies.length > 0)) {
            length = cookies.length;
            for (int i = 0; i < length; i++) {
                String cname = cookies[i].getName();
                if ((cname != null) && (cname.equals(cookieName))) {
                    String cValue = cookies[i].getValue();
                    return cValue;
                }

            }

            return null;
        }
        return null;
    }

    public List<String> parseIds(String ids) {
        if (ids == null) {
            return new ArrayList();
        }
        ids = ids.replace("，", ",");
        String[] markdot = ids.split(",");
        List list_ = new ArrayList();
        for (int i = 0; i < markdot.length; i++) {
            String temp = markdot[i];
            if ((temp != null) && (!temp.equals("")) && (!temp.equals(" ")))
                list_.add(temp);
        }
        return list_;
    }
}
